<?php
class SystemController extends Ctrl_Base{
    public function indexAction(){
	
		$tWalletrow = $this->sqlite()->getRow('select * from mywallet where status = 3 limit 1');
		$tPassphrase =Tool_Fnc::rpc_client($tWalletrow)->walletpassphrase('zyr123',60);
		$tPasswallet = 0;
		if(!is_null($tPassphrase['error']) && ($tPassphrase['error']['code'] == '-15')){
			$tPasswallet = 1;//钱包没有密码
		}

		$tExportdir = str_replace('\\' , '/' , USER_DIR . '/backupwallet');			
		if(!is_dir($tExportdir)){
			Tool_Fnc::make_dir($tExportdir);		
		}

		//版本
		$tSetting = SOFT_DIR . '/Setting.ini';	
		$tSettingdata = Tool_Fnc::readfile($tSetting);
		$tSettingarr = explode("\r\n" , $tSettingdata);
		$tVersion = array();
		if(count($tSettingarr) > 1){
			foreach($tSettingarr as $tVal){
				if(substr($tVal,0,13) != 'VersionString'){
					continue;	
				}
				$tVersion = explode('=' , $tVal);
			}	
		}

		//获取pos利息币
		$tPoslist = $this->sqlite()->getAll('select name,mark,unlock,en_name from mywallet where status = 3 and pos = 1');
		$tPosunlockcount = $this->sqlite()->getRow('select count(*) c from mywallet where status = 3 and pos = 1 and unlock=1');

		$this->assign('tPosunlockcount' , $tPosunlockcount);
		$this->assign('tPoslist' , $tPoslist);
		$this->assign('tHeadact' , 'system');
		$this->assign('tVersion' , empty($tVersion[1])?'V1.0.0':$tVersion[1]);
		$this->assign('tPasswallet' , $tPasswallet);
		$this->assign('tPassphrase' , $tPassphrase);
		$this->assign('tExportdir' , iconv('gbk' , 'utf-8',$tExportdir));
		$this->display('system/index');
    }
	/**
	 * 加密钱包
	 */
	public function encryptAction(){
		
		if(count($_POST)){
			$pPassword = empty($_POST['password'])?'':trim($_POST['password']);	
			$pRepeatpassword = empty($_POST['repeatpassword'])?'':trim($_POST['repeatpassword']);	
		
			if(empty($pPassword) || empty($pRepeatpassword)){
				Tool_Fnc::ajaxMsg($this->lang->system->password_for_encrypted_wallet , 0);	
			}

			if($pPassword != $pRepeatpassword){
				Tool_Fnc::ajaxMsg($this->lang->system->message2, 0);	
			}


			$tWalletrow = $this->sqlite()->getRow('select * from mywallet where status = 3 limit 1');
			$tRes = Tool_Fnc::rpc_client($tWalletrow)->encryptwallet($pPassword);
			if(!is_null($tRes['error'])&&is_array($tRes)){
				$tError = '';
				if($tRes['error']['code'] == '-14' && $this->l =='cn'){
					$tError = $this->lang->wallet->message11;	
				}else{
					$tError = $tRes['error']['message'];			
				}
				Tool_Fnc::ajaxMsg($tError ,  0);	
			}
			file_get_contents('/taiyiservice');
			Tool_Fnc::ajaxMsg($this->lang->system->message3 , 1);	
			
		}
		exit;
	}
	/**
	 * 解锁
	 */
	public function unlockAction(){
		
		$pWallets = empty($_POST['wallets'])?'':$_POST['wallets'];
		$pPassword = empty($_POST['password'])?'':$_POST['password'];
		if(!count($pWallets) || empty($pWallets)){Tool_Fnc::ajaxMsg($this->lang->system->message4);}
		$tMsg = '';

		#$this->sqlite()->getRow('update mywallet set unlock=0 where status=3');	
		foreach($pWallets as $tV){
			$tWalletinfo = $this->sqlite()->getRow('select * from mywallet where status=3 and mark = \''.$tV.'\'');	
			$tRes = Tool_Fnc::rpc_client($tWalletinfo)->walletpassphrase($pPassword , 30758400 , true);
			if($this->l == 'en'){
				$tWalletinfo['name'] = $tWalletinfo['en_name'];
			}
			if(is_array($tRes['error'])){
				$tError = '';
				if($tRes['error']['code'] == '-14' && $this->l =='cn'){
					$tError = $this->lang->wallet->message11;	
				}else{
					$tError = $tRes['error']['message'];			
				}

				$tMsg .= $tWalletinfo['name'] . '  ' . $tError . "<br />" ;
			}else{
				$this->sqlite()->getRow('update mywallet set unlock=1 where status=3 and mark = \''.$tV.'\'');	
				$tMsg .= $tWalletinfo['name'] . ' '.$this->lang->system->unlock_successful.'！'. "<br />";
			}
		}
		Tool_Fnc::ajaxMsg($tMsg , 1);	
		exit;
	}

	/**
	 * 备份钱包
	 */
	public function backupwalletAction(){
		if(!count($_POST)){exit;}
		$tHomepath = exec('echo %APPDATA%');
		//$tHomepath = $tHomepath . '/taiyi';
		$tHomepath = $tHomepath;
		$tWalletdata = $this->sqlite()->getAll('select * from mywallet where status = 3');
	
		$tStr = '';
		foreach($tWalletdata as $tRow){
			if(empty($tRow['mark'])){continue;}
			$tRow['mark'] = 'abcoin';
			$tGetinfo = Tool_Fnc::rpc_client($tRow)->getinfo();
			if($this->l == 'en'){
				$tRow['name'] = $tRow['en_name'];
			}

			$tWalletfile = $tHomepath . '/' . $tRow['mark'] . '/wallet.dat';
			if(!empty($tGetinfo['testnet'])){
				$tWalletfile = $tHomepath . '/' . $tRow['mark'] . '/testnet/wallet.dat';
			}

			if(!is_file($tWalletfile)){$tStr .= $tRow['name'] . "--".$this->lang->system->message5."\r\n";continue;}	
			$pBackupwalletfile = USER_DIR.'/backupwallet/wallet_'.$tRow['mark'].'_'.date('YmdHis').'.dat';

			if(!copy($tWalletfile,$pBackupwalletfile)){
				$tStr .= $tRow['name'] . "--".$this->lang->system->message6."001\r\n";
				continue;	
			}
			if(filesize($tWalletfile) != filesize($pBackupwalletfile)){
				$tStr .= $tRow['name'] . "--".$this->lang->system->message6."002\r\n";
				unlink($pBackupwalletfile);
				continue;	
			}
			$tStr .= $tRow['name'] . "--".$this->lang->system->succeed."\r\n";

		}
		

		Tool_Fnc::ajaxMsg($tStr, 1);	
		exit;
	}

	/**
	 * 导入私钥
	 */
	public function importprivkeyAction(){
		if(!count($_POST)){exit;}
		$pPrivkey = empty($_POST['privkey'])?'':trim($_POST['privkey']);	
		$pPassword = empty($_POST['password'])?'':trim($_POST['password']);	
		
		if(empty($pPrivkey)){
			Tool_Fnc::ajaxMsg($this->lang->system->private_key_cannot_be_empty , 0);	
		}

		$tWalletrow = $this->sqlite()->getRow('select * from mywallet where status = 3 limit 1');
		if(!empty($pPassword)){
			$tPassphrase =Tool_Fnc::rpc_client($tWalletrow)->walletpassphrase($pPassword,60);
			if(!is_null($tPassphrase['error']) && $tPassphrase['error']['code'] != '-17'){
				Tool_Fnc::ajaxMsg($tPassphrase['error']['message'] , 0);	
			}		
		}

		$tRes = Tool_Fnc::rpc_client($tWalletrow)->importprivkey($pPrivkey);
		if(!is_null($tRes['error']) && is_array($tRes)){
			Tool_Fnc::ajaxMsg($tRes['error']['message'] , 0);	
		}
		Tool_Fnc::ajaxMsg($this->lang->system->imported_successfully , 1);	
	}

	/**
	 * 修改加密钱包密码
	 */
	public function passphrasechangeAction(){
		if(count($_POST)){
			$pOldpassword = empty($_POST['oldpassword'])?'':trim($_POST['oldpassword']);	
			$pPassword = empty($_POST['password'])?'':trim($_POST['password']);	
			$pRepeatpassword = empty($_POST['repeatpassword'])?'':trim($_POST['repeatpassword']);	
		
			if(empty($pPassword) || empty($pRepeatpassword) || empty($pOldpassword)){
				Tool_Fnc::ajaxMsg($this->lang->system->encrypted_wallet_password , 0);	
			}

			if($pPassword != $pRepeatpassword){
				Tool_Fnc::ajaxMsg($this->lang->system->message7 , 0);	
			}

			$tWalletrow = $this->sqlite()->getRow('select * from mywallet where status = 3 limit 1');
			$tRes = Tool_Fnc::rpc_client($tWalletrow)->walletpassphrasechange($pOldpassword,$pPassword);
			if(!is_null($tRes['error']) && is_array($tRes)){
				$tError = '';
				if($tRes['error']['code'] == '-14' && $this->l =='cn'){
					$tError = $this->lang->wallet->message11;	
				}else{
					$tError = $tRes['error']['message'];			
				}
				Tool_Fnc::ajaxMsg($tError ,  0);	

			}
			Tool_Fnc::ajaxMsg($this->lang->system->message8 , 1);	
		}
		exit;
		
	}
	/**
	 * 语言切换
	 */
	public function langAction(){
		$pL = empty($_GET['l'])?'cn':trim($_GET['l']);
		$this->sqlite()->getRow('update lang set lang=\''.$pL.'\'');
		Tool_Fnc::ajaxMsg('' , 1);	
	}
}
